import { BasicColumn, FormSchema } from '@/components/Table';
import { useDictStoreWithOut } from '@/store/modules/dict';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';

const useDict = useDictStoreWithOut();

const sourceState = useDict.getDict('1002');

export const searchFormSchema: FormSchema[] = [
  {
    field: 'sourceStatus',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: sourceState,
    },
    colProps: { span: 8 },
  },
  {
    field: 'sourceName',
    label: '名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'sourceTheme',
    label: '主题',
    component: 'Input',
    colProps: { span: 8 },
  },
];

export const columns: BasicColumn[] = [
  {
    title: '资源名称',
    dataIndex: 'sourceName',
    align: 'left',
    fixed: 'left',
    width: 200,
  },
  {
    title: '主题',
    dataIndex: 'sourceTheme',
    fixed: 'left',
    width: 200,
  },
  {
    title: '父主题',
    dataIndex: 'parentName',
    width: 100,
  },
  {
    title: '主标签',
    dataIndex: 'sourceMainTag',
    width: 200,
  },
  {
    title: '同级标签',
    dataIndex: 'sourcePeerTag',
    width: 120,
  },
  {
    title: '执行次序',
    dataIndex: 'sourceExecuteOrder',
    width: 80,
  },
  {
    title: 'ID脚本',
    dataIndex: 'keyScriptName',
    width: 120,
  },
  {
    title: '目标源编码',
    dataIndex: 'targetCode',
    width: 120,
  },
  /*{
    title: '入库前置脚本',
    dataIndex: 'databasePreScriptName',
    width: 120,
  },*/
  {
    title: '状态',
    dataIndex: 'sourceStatus',
    width: 120,
    customRender: ({ record }) => {
      const status = record.sourceStatus;
      const enable = ~~status === 10021001;
      const color = enable ? 'green' : 'red';
      const text = enable ? '启用' : '停用';
      return h(Tag, { color: color }, () => text);
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'sourceId',
    label: 'ID',
    component: 'Input',
    ifShow: false,
  },
  {
    field: 'cloudOption',
    label: '云类型聚合字段',
    component: 'Input',
    ifShow: false,
  },
  {
    field: 'sourceName',
    label: '名称',
    required: true,
    component: 'Input',
  },
  {
    field: 'parentId',
    label: '主资源',
    component: 'Select',
    componentProps: {
      fieldNames: {
        label: 'sourceName',
        value: 'sourceId',
      },
    },
  },
  {
    field: 'sourceTheme',
    label: '主题',
    required: true,
    component: 'Input',
  },
  {
    field: 'sourceMainTag',
    label: '主标签',
    component: 'Input',
  },
  {
    field: 'sourcePeerTag',
    label: '同级标签',
    component: 'Input',
    componentProps: {
      placeholder: '多个同级标签用":"拼接',
    },
  },
  {
    field: 'sourceExecuteOrder',
    label: '执行次序',
    component: 'InputNumber',
    defaultValue: 0,
  },
  {
    field: 'sourceStatus',
    label: '启用情况',
    component: 'Select',
    defaultValue: '10021001',
    componentProps: {
      options: sourceState,
    },
  },
  {
    field: 'targetId',
    label: '目标端',
    component: 'Select',
    required: true,
    componentProps: {
      showSearch: true,
      filterOption: (input: string, option: any) => {
        return option.label.includes(input);
      },
    },
  },
  {
    field: 'keyScriptId',
    label: 'ID脚本',
    component: 'Select',
    required: true,
  },
  {
    field: 'pushPreScript',
    label: '推送前置脚本',
    component: 'Select',
  },
  {
    field: 'databasePreScript',
    label: '入库前置脚本',
    component: 'Select',
  },
];
